package com.t216.dm216.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.t216.dm216.mapper.DmItemCommentMapper;
import com.t216.dm216.pojo.DmItemComment;
import com.t216.dm216.pojo.DmUser;
import com.t216.dm216.pojo.dto.DmItemCommentDTO;

import com.t216.dm216.service.DmItemCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class DmItemCommentServiceImpl implements DmItemCommentService {
    @Resource
    private DmItemCommentMapper dmItemCommentMapper;
    @Override
    public List<DmItemCommentDTO> getCommentsByItemId(Long itemId) {
        MPJLambdaWrapper<DmItemComment> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(DmItemComment::getItemId, DmItemComment::getUserId, DmItemComment::getScore, DmItemComment::getContent, DmItemComment::getCreatedTime);
        queryWrapper.selectAs(DmUser::getNickName, DmItemCommentDTO::getUserName);
        queryWrapper.innerJoin(DmUser.class,DmUser::getId,DmItemComment::getUserId);
        queryWrapper.eq(DmItemComment::getItemId,itemId);
        return dmItemCommentMapper.selectJoinList(DmItemCommentDTO.class,queryWrapper);
    }

    @Override
    public boolean addItemComment(DmItemComment dmItemComment) {
        return dmItemCommentMapper.insert(dmItemComment)>0;
    }
}
